package com.webdrider.manager;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.common.model.util.UserType;
import com.common.security.exceptions.ManagerException;
import com.common.security.exceptions.UserException;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.webdrider.manager.action.user.UserManager;
import com.webdrider.model.action.user.User;

public class UserManagerTest {

	private final LocalServiceTestHelper helper =
			new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
	
	@Before
	public void setUp() {
		helper.setUp();
	}
	
	@After
	public void tearDown() {
		helper.tearDown();
	}
	
	@Test
	public void testSaveUserSuccesfully() throws ManagerException {
		UserManager manager = new UserManager();
		User user = new User();
		
		user.setUsername("random username");
		user.setPassword("random password");
		user.setUserType(UserType.USER);
		
		Assert.assertEquals(0, manager.getAll().size());
		manager.save(user);
		Assert.assertEquals(1, manager.getAll().size());
	}
	
	@Test
	public void testGetUserByUsernameAndPasswordSuccesfully() throws ManagerException, UserException {
		UserManager manager = new UserManager();
		User user = new User();
		
		user.setUsername("random username");
		user.setPassword("random password");
		user.setUserType(UserType.USER);
		
		Assert.assertEquals(0, manager.getAll().size());
		manager.save(user);
		Assert.assertEquals(1, manager.getAll().size());
		
		User resultUser = manager.getUser("random username", "random password");
		Assert.assertNotNull(resultUser);
		Assert.assertEquals("random username", user.getUsername());
		Assert.assertEquals("random password", resultUser.getPassword());
		Assert.assertEquals(UserType.USER, resultUser.getUserType());
		Assert.assertFalse(user.isOnline());
		Assert.assertNotNull(resultUser.getId());
	}
	
	@Test
	public void testUserLoginSuccesfully() throws ManagerException, UserException {
		UserManager manager = new UserManager();
		User user = new User();
		
		user.setUsername("random username");
		user.setPassword("random password");
		user.setUserType(UserType.USER);
		
		Assert.assertEquals(0, manager.getAll().size());
		manager.save(user);
		Assert.assertEquals(1, manager.getAll().size());
		
		User resultUser = manager.getUser("random username", "random password");
		Assert.assertFalse(resultUser.isOnline());
		
		resultUser = manager.login("random username", "random password");
		Assert.assertNotNull(resultUser);
		Assert.assertTrue(resultUser.isOnline());
	}
	
	@Test
	public void testUserLogoutSuccesfully() throws ManagerException, UserException {
		UserManager manager = new UserManager();
		User user = new User();
		
		user.setUsername("random username");
		user.setPassword("random password");
		user.setUserType(UserType.USER);
		
		Assert.assertEquals(0, manager.getAll().size());
		manager.save(user);
		Assert.assertEquals(1, manager.getAll().size());
		
		User resultUser = manager.getUser("random username", "random password");
		Assert.assertFalse(resultUser.isOnline());
		
		resultUser = manager.login("random username", "random password");
		Assert.assertNotNull(resultUser);
		Assert.assertNotNull(resultUser.getId());
		Assert.assertTrue(resultUser.isOnline());
		
		manager.logout(resultUser.getId());
		resultUser = manager.getById(resultUser.getId());
		Assert.assertNotNull(resultUser);
		Assert.assertFalse(resultUser.isOnline());
	}
}
